package com.wyw.leetcode.learning.simple;

/**
 * leetcode topic 167
 * 两数之和-输入有序数组
 *
 * @Author Mr Wu （yewen.wu.china@gmail.com）
 * @Date 2021/11/8 10:22
 */
public class Topic167 {
    public static void main(String[] args) {
        int[] nums = {2,7,11,15};
        twoSum(nums, 9);
//        twoSum1(nums, 9);
    }

    public static int[] twoSum(int[] numbers, int target) {
        int length = numbers.length;
        int[] result = new int[2];
        for(int i = 0; i< length - 1; i++) {
            for(int j =i+1; j<length; j++) {
                if(numbers[i]+ numbers[j] == target) {
                    result[0] = i + 1;
                    result[1] = j + 1;
                    return result;
                }
            }
        }
        return result;
    }
    public static int[] twoSum1(int[] numbers, int target) {
        int i=0;
        int j = numbers.length - 1;
        while(i < j) {
            int sum = numbers[i] + numbers[j];
            if(sum > target) {
                j--;
            } else if(sum < target) {
                i++;
            } else {
                return new int[] {i+1,j+1};
            }
        }
        return new int[] {-1,-1};
    }
}
